package ishacrm.client;



import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Checkin extends Composite {

	private static CheckinUiBinder uiBinder = GWT.create(CheckinUiBinder.class);

	interface CheckinUiBinder extends UiBinder<Widget, Checkin> {
	}

	public Checkin() {
		initWidget(uiBinder.createAndBindUi(this));
		
		programListBox.addItem("Fetching ...");
		programListBox.setSelectedIndex(0);
		
		Timer timer = new Timer() {
			public void run() {		
				programListBox.clear();
				programListBox.addItem("Sathsang @ Singapore (5 Apr)");
				programListBox.addItem("Yogasanas @ Singapore Session 2 (5 Apr)");
			}
		};
		
		timer.schedule(2000);	
				
		
	}
	
	@UiHandler("hideDetailsCheckBox")
	void onClick(ClickEvent event) {
		renderMatches(qsTextBox.getText(),  ! hideDetailsCheckBox.getValue());
	}
	
	@UiHandler("qsTextBox")
	void onValueChange(ValueChangeEvent<String> event) {
		matchesHtmlPanel.clear();
		numMatchesLabel.setText("Searching ...");
		
		Timer timer = new Timer() {
			
			@Override
			public void run() {				
				renderMatches(qsTextBox.getText(),  ! hideDetailsCheckBox.getValue());
			}
		};
		
		timer.schedule(1000);    	
    }
	
	@UiHandler("qsTextBox")
	 void onKeyUp(KeyUpEvent event) {
		matchesHtmlPanel.clear();
		numMatchesLabel.setText("Searching ...");
		
		Timer timer = new Timer() {
			
			@Override
			public void run() {				
				renderMatches(qsTextBox.getText(), ! hideDetailsCheckBox.getValue());
			}
		};
		
		timer.schedule(1000);   
    }
	
	void renderMatches(String searchStr, boolean showDetail) {
		numMatchesLabel.setText("");
		matchesHtmlPanel.clear();
		String text = qsTextBox.getText();
		if (text.equals(""))
			return;
		
		List<ContactDetailProp> matches = getMatches(searchStr);
		numMatchesLabel.setText(matches.size() + " match(es)");
		
		for (ContactDetailProp contactDetailProp : matches) {
			MemberSummary memberSummary = new MemberSummary(contactDetailProp, showDetail);
			matchesHtmlPanel.add(memberSummary);
		}						
	}
	
	List<ContactDetailProp> getMatches(String searchStr) {
		ContactDetailProp sathya = new ContactDetailProp();
		sathya.name = "Sathyanarayanan Thilakan";
		sathya.email = "sathya.t@ishafoundation.org";
		sathya.phones = "+6593232152, +6563083013 +6565227030";
		sathya.centers = "Singapore";
		sathya.labels.add("Shambhavi");
		sathya.labels.add("BSP");
		sathya.labels.add("Shoonya");
		sathya.labels.add("Samyama");
		
		ContactDetailProp thulasi = new ContactDetailProp();
		thulasi.name = "Thulasidhar Kosalram";
		thulasi.email = "thulasidhar@gmail.com";
		thulasi.phones = "+6592705371,, +6531612535";
		thulasi.centers = "Singapore";
		thulasi.labels.add("Shambhavi");
		thulasi.labels.add("BSP");
		thulasi.labels.add("Shoonya");
		thulasi.labels.add("Samyama");
		
		ContactDetailProp amit = new ContactDetailProp();
		amit.name = "Amit Thakur";
		amit.email = "er.amitthakur@gmail.com";
		amit.centers = "Singapore";
		amit.labels.add("Shambhavi");
		
		ContactDetailProp sharmila = new ContactDetailProp();
		sharmila.name = "Sharmila Napa";
		sharmila.email = "sharmila@bhairaviyoga.sg";
		sharmila.phones = "+6581248184, +6531612535";
		sharmila.centers = "Singapore";
		sharmila.labels.add("Shambhavi");
		sharmila.labels.add("BSP");
		sharmila.labels.add("Shoonya");
		sharmila.labels.add("Samyama");
		
		ContactDetailProp sowmya = new ContactDetailProp();
		sowmya.name = "Sowmya Ramakrishnan";
		sowmya.email = "sowmya.ram@gmail.com";
		sowmya.phones = "+6591252026, +6565227030";
		sowmya.centers = "Singapore";
		sowmya.labels.add("Shambhavi");
		
		ContactDetailProp shiva = new ContactDetailProp();
		shiva.name = "Shiva Jawaji";
		shiva.email = "sjawaji@gmail.com";
		shiva.phones = "+61469218170";
		shiva.centers = "Australia";
		shiva.labels.add("Shambhavi");
		shiva.labels.add("BSP");
		shiva.labels.add("Shoonya");
		
		List<ContactDetailProp> all = new ArrayList<>();
		all.add(sathya);
		all.add(thulasi);
		all.add(sharmila);
		all.add(sowmya);
		all.add(amit);
		all.add(shiva);
		
		searchStr = searchStr.toLowerCase();
		
		List<ContactDetailProp> matches = new ArrayList<>();
		for (ContactDetailProp c : all) {
			if (c.name.contains(searchStr) ||
			 ((c.email != null) && c.email.contains(searchStr)) || 
			 ((c.phones != null && c.phones.contains(searchStr))))
			 
			 matches.add(c);
		}
		
		return matches;				
	}
				
	@UiField TextBox qsTextBox;
	@UiField HTMLPanel matchesHtmlPanel;
	@UiField ListBox programListBox;
	@UiField SpanElement searchStatus;
	@UiField Label numMatchesLabel;
	@UiField CheckBox hideDetailsCheckBox;
}
